
                /*********************************************
                **                                          **
                **                                          **
                **               g.l.d.b.C                  **
                **                                          **
                **                                          **
                *********************************************/
                /*********************************************
                **                                          **
                **         Author Jean-Marc Adamo           **
                **       Copyright (C) 1997-2008 JMA        **
                *********************************************/


/*
** Direct Access Global and Blocking Global Read/Write Functions 
** Needs 3 processors.
*/

#include "arch.h"

Global_id gid;
Coll_id cid1, cid2, cid3;

void init_gid(int, char **){
   gid.set();
   cid1.set();
   cid2.set();
   cid3.set();
   fprintf(Sdf::trace, "from node %d: gid=%d, cid1=%d, cid2=%d, cid3=%d\n", 
           Sdf::PROC_ID, gid.get(), cid1.get(), cid2.get(), cid3.get());
   fflush(Sdf::trace);
}

class Root_thread: public Thread{
   public:
        Root_thread()
                : Thread((Tmf0_ptr)&Root_thread::body){}

        void body(){
           fprintf(Sdf::trace, "from node%d: this is my rank\n", Sdf::PROC_ID);
           fflush(Sdf::trace);
           char source_data[21];
           char rw_target[21];
           char dest_data[21];

           int s;
           for(s=0; s<21; s++)
              source_data[s]  = Sdf::PROC_ID;
           for(s=0; s<21; s++)
              rw_target[s]    = 50+Sdf::PROC_ID;
           for(s=0; s<21; s++)
              dest_data[s]    = 100+Sdf::PROC_ID;

           int i;
           for(i=0; i<21; i++)
             fprintf(Sdf::trace, "node%d: s[%d]=%d, t[%d]=%d, d[%d]=%d\n",
                     Sdf::PROC_ID, i, source_data[i], i, rw_target[i],i, dest_data[i]);
           fflush(Sdf::trace);
           Global<char> global(gid, rw_target);
           Sdf::barrier(cid1);
            //global synchonization of Global constructors has become useless
            //starting from version 5

           global.write(Sdf::PROC_ID, 0, source_data[0], 21, 0);

           Sdf::barrier(cid2);


           global.read(Sdf::PROC_ID, 0, dest_data[0], 21, 0);

           for(i=0; i<21; i++)
             fprintf(Sdf::trace, "node%d: s[%d]=%d, t[%d]=%d, d[%d]=%d\n",
                          Sdf::PROC_ID, i, source_data[i], i, rw_target[i],i, dest_data[i]);
           fflush(Sdf::trace);

           Sdf::barrier(cid3);

        }
};

MAIN(Root_thread, init_gid, Sdf::empty, Sdf::empty)
